<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col :span="4">
          <el-button type="success" @click="isShowAdd=true">添加角色</el-button>
        </el-col>
        <el-col :span="10">
          <span>删除和编辑没做</span>
        </el-col>
      </el-row>

      <el-table border stripe :data="rolesList">
        <!-- 展开列 -->
        <el-table-column type="expand">
          <template slot-scope="scope">
            <!-- 循环第一列 -->
            <el-row
              :class="['bdbottom','vcenter',index1===0 ? 'bdtop' : '']"
              v-for="(item1,index1) in scope.row.children"
              :key="item1.id"
            >
              <el-col :span="5">
                <el-tag closable @close="delteRightByRolesId(scope.row,item1.id)">{{item1.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <!-- 循环第二轮 -->
                <el-row
                  :class="[index2===0 ? '' : 'bdtop','vcenter']"
                  v-for="(item2,index2) in item1.children"
                  :key="item2.id"
                >
                  <el-col :span="6">
                    <el-tag
                      closable
                      @close="delteRightByRolesId(scope.row,item2.id)"
                    >{{item2.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <!-- 循环第三轮 -->
                    <el-tag
                      type="warning"
                      v-for="(item3) in item2.children"
                      :key="item3.id"
                      closable
                      @close="delteRightByRolesId(scope.row,item3.id)"
                    >{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button-group>
              <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="deleteRoles(scope.row.id)"
              >删除</el-button>
              <el-button
                size="mini"
                type="warning"
                icon="el-icon-setting"
                @click="isShowSetRoleRightDialog=true"
              >分配权限</el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <addRolesDialog :isShow="isShowAdd" @setIsShow="closeAddFrom" @Refresh="getRolesList"></addRolesDialog>
    <setRoleRightDialog :isShow="isShowSetRoleRightDialog" @setIsShow="closeSetRoleRightDialog"></setRoleRightDialog>
  </div>
</template>

<script>
import addRolesDialog from './addRolesDialog.vue'
import setRoleRightDialog from './setRoleRightDialog.vue'
export default {
  components: {
    addRolesDialog,
    setRoleRightDialog
  },
  data() {
    return {
      rolesList: [],
      isShowAdd: false,
      //是否显示分配权限页面
      isShowSetRoleRightDialog: false
    }
  },
  methods: {
    async getRolesList() {
      const { data: res } = await this.$http.get('roles')
      if (res.meta.status === 200) {
        this.rolesList = res.data
      } else {
        this.$message.error(res.meta.msg)
      }
    },
    //关闭添加页面
    closeAddFrom() {
      this.isShowAdd = false
    },
    //关闭分配权限页面
    closeSetRoleRightDialog() {
      this.isShowSetRoleRightDialog = false
    },
    //删除角色
    deleteRoles(id) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await this.$http.delete('roles/' + id)
          if (res.meta.status === 200) {
            this.$message.success('删除成功!')
            this.getRolesList()
          } else {
            this.$message.error(res.meta.msg)
          }
        })
        .catch(() => {
          this.$message.info('已取消删除')
        })
    },
    //删除某个角色的具体权限
    delteRightByRolesId(role, rightId) {
      this.$confirm('此操作将永久取消该权限吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await this.$http.delete(
            `roles/${role.id}/rights/${rightId}`
          )
          if (res.meta.status === 200) {
            this.$message.success('取消成功!')
            //返回的数据为当前最新的数据
            role.children = res.data
          } else {
            this.$message.error(res.meta.msg)
          }
        })
        .catch(() => {
          this.$message.info('已取消删除')
        })
    }
  },
  created() {
    this.getRolesList()
  }
}
</script>

<style lang="less" scoped>
.el-tag {
  margin: 7px;
}

.bdtop {
  border-top: 2px solid #eee;
}

.bdbottom {
  border-bottom: 2px solid #eee;
}

.vcenter {
  display: flex;
  align-items: center;
}
</style>